package user

import (
	"context"
	"fmt"

	"go-zero-admin/apps/user/api/internal/svc"
	"go-zero-admin/apps/user/api/internal/types"
	"go-zero-admin/apps/user/rpc/user"
	"go-zero-admin/pkg/utils"

	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserPermMenuLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetUserPermMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserPermMenuLogic {
	return &GetUserPermMenuLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetUserPermMenuLogic) GetUserPermMenu() (resp *types.UserPermMenuResp, err error) {
	userId := utils.GetUserId(l.ctx)

	var menus []types.Menu
	res, err := l.svcCtx.UserRPC.Permmenu(l.ctx, &user.UserPermMenuReq{
		UserId: int64(userId),
	})
	if err != nil {
		return nil, err
	}
	err = copier.Copy(&menus, &res.Menus)
	if err != nil {
		fmt.Println("==============> +9999999999", err)

		return nil, err
	}
	return &types.UserPermMenuResp{
		Menus: menus,
		Perms: res.Perms,
	}, nil
}
